package com.ngc.mall.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.CategoryDomain;
import com.ngc.mall.dto.CategoryDTO;
import com.ngc.mall.param.SpuCategoryParam;
import com.ngc.mall.service.ICategoryService;
import com.ngc.mall.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品分类
 *
 * @author: chenxiaolin
 * @date 2023/4/4 8:57
**/
@RestController
@RequestMapping("/category")
@Api(tags = "商品分类")
public class CategoryController {
    @Resource
    private ICategoryService service;

    /**
     * 获取所有商品分类
     * @return
     */
    @GetMapping("/all")
    @ApiOperation(value="获取所有商品分类")
    public ResponseResult<List<CategoryVO>> selectAll(){
        return ResponseResult.success(this.service.selectAllCategory());
    };

    /**
     * 创建商品分类
     * @param param
     * @return
     */
    @PostMapping("")
    @ApiOperation(value="创建商品分类")
    public ResponseResult<Long> createCategory(@RequestBody SpuCategoryParam param){
        CategoryDTO dto= CategoryDTO
                .builder()
                .isEnabled(param.getIsEnabled())
                .sort(param.getSort())
                .name(param.getName())
                .pid(param.getPid())
                .img(param.getImg())
                .build();
        return ResponseResult.success(this.service.createCategory(dto));
    }

    /**
     * 根据id获取商品分类详情
     */
    @GetMapping("/id/{id}")
    @ApiOperation(value="根据id获取商品详情")
    public ResponseResult<CategoryDomain> getDetailById(@PathVariable Long id){
        CategoryDomain domain=this.service.getById(id);
        System.out.println(domain);
        return ResponseResult.success(this.service.getById(id));
    }

    /**
     * 根据id集合删除指定商品分类及其子分类
     * @return
     */
    @DeleteMapping("/id/{id}")
    @ApiOperation(value = "删除商品分类及其子分类")
    public ResponseResult<Boolean> deleteCategories(@PathVariable Long id){
        return ResponseResult.success(this.service.deleteById(id));
    }

    /**
     * 根据id编辑商品分类
     * @param id
     * @param param
     * @return
     */
    @PutMapping("/id/{id}")
    @ApiOperation(value = "根据id编辑商品分类")
    public ResponseResult<Boolean> updateDetailById(@PathVariable Long id,@RequestBody SpuCategoryParam param){
        return ResponseResult.success(this.service.update(
                Wrappers.<CategoryDomain>lambdaUpdate()
                        .eq(CategoryDomain::getId,id)
                        .set(CategoryDomain::getImg,param.getImg())
                        .set(CategoryDomain::getIsEnabled,param.getIsEnabled())
                        .set(CategoryDomain::getPid,param.getPid())
                        .set(CategoryDomain::getSort,param.getSort())
        ));
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PutMapping("/disabled/{id}")
    @ApiOperation(value = "停用商品分类")
    public ResponseResult<Boolean> updateToDisabled(@PathVariable Long id){
        return ResponseResult.success(this.service.update(
                Wrappers.<CategoryDomain>lambdaUpdate().eq(CategoryDomain::getId,id).set(CategoryDomain::getIsEnabled,false)));
    }

    /**
     * 启用
     * @return
     */
    @PutMapping("/enabled/{id}")
    @ApiOperation(value = "启用商品分类")
    public ResponseResult<Boolean> updateToEnabled(@PathVariable Long id){
      return ResponseResult.success(this.service.update(
              Wrappers.<CategoryDomain>lambdaUpdate().eq(CategoryDomain::getId,id).set(CategoryDomain::getIsEnabled,true)));
    }

}
